#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <list>
#include "base/pmvs/findMatch.h"
#include "base/pmvs/option.h"
#include "base/cmvs/bundle.h"
//#include "cmvs.hpp"
#include "genOption.h"

using namespace PMVS3;
using namespace std;

int main(int argc, char **argv) {

    if(argc<2){
        cerr<<"Usage: "<<argv[0]<<" Input_file"<<
              "argv[0]/Dense [option outputFile]"<<"\n";
        exit(1);
    }
    string prefix;
    if(argc==2)
    {
        string inputFolder=argv[1];
        int pos1=inputFolder.find_last_of('/');
        string b=inputFolder.substr(0,pos1)+"/Dense";
        char  cmds[2048];
        sprintf(cmds, "mkdir -p '%s'", b.c_str());
        system(cmds);
        prefix=b+"/";
        std::cout<<prefix<<endl;
    }
    if(argc==3)
    {
        string inputFolder=argv[2];
        char  cmds[2048];
        sprintf(cmds, "mkdir -p '%s'", inputFolder.c_str());
        system(cmds);
        prefix=inputFolder+"/";
    }


    inputData inputFile;
    inputFile.getInputFile(argv[1],prefix);

    //    char optionfile[20];
    //    sprintf(optionfile,"dense");   //dense output
    //    std::cout << std::endl;

    //CMVS
    int maximage = 50;
    int CPU = 4;
    const float scoreRatioThreshold = 0.7f;
    const float coverageThreshold = 0.7f;
    const int iNumForScore = 4;
    const int pnumThreshold = 0;

    CMVS::Cbundle bundle= inputFile.getBundle();
    bundle.run(prefix, maximage, iNumForScore,
               scoreRatioThreshold, coverageThreshold,
               pnumThreshold, CPU);


    //PMVS
//        std::vector<std::vector<Patch::Ppatch> > ppatchesall;

    char optionfile[20];
    sprintf(optionfile,"dense");   //dense output
    std::vector<std::vector<double> > vertexall;
    for(int i=0; i<bundle.m_timages.size(); i++){
        PMVS3::Soption option;
        option.init(prefix, bundle.m_timages[i], bundle.m_oimages[i],bundle.m_vis);

        PMVS3::CfindMatch findMatch;
        findMatch.init(option,bundle.m_intri,bundle.m_extri);
        findMatch.run();

        bool bExportPLY = true;
        bool bExportPatch = false;
        bool bExportPSet = false;

        findMatch.write(vertexall, bExportPLY, bExportPatch, bExportPSet);


        if(i == (bundle.m_timages.size()-1))
        {
            char buffer[1024];
            sprintf(buffer, "%s%s", prefix.c_str(), optionfile);
            std::cout<<buffer<<endl;
            findMatch.write(vertexall,buffer, bExportPLY, bExportPatch, bExportPSet);
        }
    }


    //    PMVS3::Soption option;
    //    option.init(prefix, bundle.m_timageall, bundle.m_oimagesall,bundle.m_vis);

    //    PMVS3::CfindMatch findMatch;
    //    findMatch.init(option,bundle.m_intri,bundle.m_extri);
    //    findMatch.run();


    //    bool bExportPLY = true;
    //    bool bExportPatch = false;
    //    bool bExportPSet = false;

    //    char buffer[1024];
    //    sprintf(buffer, "%s%s", prefix.c_str(), optionfile);
    //    std::cout<<buffer<<endl;
    //    findMatch.write(buffer, bExportPLY, bExportPatch, bExportPSet);


}
